"""
模板过滤器
"""
from flask import current_app, session, g
from info.models import User
import functools

def do_index_class(num):
    if num == 1:
        return "first"
    elif num == 2:
        return "second"
    elif num == 3:
        return "third"
    else:
        return ""

def user_login_data(view_func):
    @functools.wraps(view_func)# url_map蓝图名称处理
    def wrapper(*args, **kwargs):
        """session状态保持"""
        # 获取到当前登录用户的id
        user_id = session.get("user_id")
        # 通过id获取用户信息
        user = None
        if user_id:
            # 表示已经获取到user_id，所以此时需要查询用户信息，返回给前端(比如展示右上角昵称信息)
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

        g.user = user
        return view_func(*args, **kwargs)
    return wrapper